/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 *
 * File: ca/autotrader/compare/widgets/PageToolbar.js
 * Purpose: Provides custom implimintation of the pages toolbar widget.
 *
 * Namespace: ca.autotrader.compare
 * Requires:ExtJS,
 *          ASC library - net/autodata/ascjs-ext-debug.js
 */
Ext.ns('net.autodata.compare.toolbars');
(function() {
    /**
    * Creates the navigation button data from a data map defined on the ASC_config object.
    */
    var createButtonData = undefined;
    (function() {
        var session = net.autodata.Session.getInstance();
        var navData = session.getPageUrls();

        createButtonData = function() {
            var pages = new Ext.util.MixedCollection(false);
            var emptyUrl = 'javascript:void(0)';
            pages.addAll([
    		    { id: 'results', text: ASC.message('net.autodata.compare.pagetoolbar.label.results'), url: navData['results'] || emptyUrl },
    		    { id: 'advantages', text: ASC.message('net.autodata.compare.pagetoolbar.label.advantages'), url: navData['advantages'] || emptyUrl },
    		    { id: 'photos', text: ASC.message('net.autodata.compare.pagetoolbar.label.photos'), url: navData['photos'] || emptyUrl },
    		    { id: 'editorial', text: ASC.message('net.autodata.compare.pagetoolbar.label.editorial'), url: navData['editorial'] || emptyUrl }
    		]);
            return pages;
        };
    })();

    /**
    * Page header toolbar
    * @class PageToolbar
    * @extends Ext.Toolbar
    * @namespace net.autodata.compare.toolbars
    */
    var View = Ext.extend(net.autodata.AbstractView, {
        /**
        * @property lblTpl
        * @type Ext.Template
        */
        lblTpl: undefined,

        /**
        * @property activeKey
        * @type String
        */
        activeKey: undefined,

        /**
        * @method constructor
        * @param {Object} cfg
        */
        constructor: function(cfg) {
            View.superclass.ctor.apply(this, arguments);
        },

        /**
        * @method init
        */
        init: function() {
            View.superclass.init.apply(this, arguments);

            if (!this.lblTpl) {
                this.lblTpl = new Ext.Template('<div style="margin-bottom:4px;"><b>{text}</b> {value}</div>', { compiled: true, disableFormats: true });
            }
        },

        /**
        * Turns off the button toggled property on all toolbar buttons.
        * @method removeToggles
        */
        removeToggles: function() {
            var tb = this.component;
            if (tb != null) {
                if (this.data === undefined) {
                    return;
                }

                this.data.eachKey(function(key, value) {
                    var button = tb.getComponent(key);
                    if (button != null) {
                        button.toggle(false, true);
                    }
                }, this);
            }
        },

        /**
        * Builds toolbar's inner components.
        * @method buildComponents
        */
        buildComponents: function() {
            var tb = this.component;

            tb.removeAll(true);

            if (tb.shownav && this.data === undefined) {
                this.data = this.createButtonData();
            }

            tb.addItem({
                xtype: 'tbtext',
                text: ASC.message('net.autodata.compare.pagetoolbar.label.compare-vehicle'),
                style: {
                    padding: '0 20px 0 5px',
                    margin: '0 0 2px 0'
                }
            });

            if (this.data !== undefined) {
                this.data.eachKey(function(key, value) {
                    if (this.activeGroup === undefined) {
                        this.activeGroup = key;
                    }

                    tb.addButton({
                        text: value.text,
                        xtype: 'a-btn-toolbar',
                        itemId: key,
                        hide: false,
                        enableToggle: true,
                        listeners: {
                            'click': this.onButtonClick.createDelegate(this, [key, value.url], 1)
                        }
                    });
                }, this);
            }

            tb.addFill();
        },

        /**
        * Creates the navigation button data.
        */
        createButtonData: createButtonData,

        /**
        * View's render method
        * @method render
        * @private
        */
        render: function() {
            var tb = this.component;

            if (tb.shownav) {
                tb.el.addClass('page-toolbar-wnav');
            }

            this.buildComponents();

            if (tb.boxReady) {
                tb.doLayout(false, true);
            }

            if (this.activeKey !== undefined) {
                var button = tb.getComponent(this.activeKey);
                if (button != null) {
                    button.toggle(true, true);
                }
            }
        },

        //Event Handlers,

        /**
        * Store load's event handler that applies a filter after the store loads.
        * @method onButtonClick
        * @param {net.autodata.compare.toolbars.PageToolbarView} this The view scope
        * @param {Ext.Button} button Ext Button object that triggered the click event.
        * @param {string} groupID The groupID name.
        * @param (RegEx) filter The regular expression filter.
        * @private
        */
        onButtonClick: function(button, key, url) {
            this.removeToggles()
            button.toggle(true, true);

            if (url) {
                if (key !== this.activeKey) {
                    var session = net.autodata.Session.getInstance();
                    var vehicles = session.vehicles;

                    if (vehicles) {
                        var baseVehicle = vehicles.getBaseVehicle();
                        if (baseVehicle) {
                            var obj = {
                                baseAcode: baseVehicle.acode,
                                competitors: []
                            }

                            var selectedColour = baseVehicle.getSelectedSwatch();
                            if (selectedColour && selectedColour.shortDesc) {
                                ASC.apply(obj, {
                                    colourShortDesc: selectedColour.shortDesc
                                });
                            }

                            vehicles.eachCompetitor(function(competitor) {
                                if (competitor) {
                                    if (competitor.sponsored) {
                                        ASC.apply(obj, {
                                            sponsoredAcode: competitor.acode
                                        });
                                    }
                                    obj.competitors.push(competitor.acode);
                                } else {
                                    obj.competitors.push('');
                                }
                            });

                            var form = ASC.Serializer.objectToForm({
                                action: String.format(url, ASC.cfg.getLocaleContextPath()),
                                method: 'GET',
                                params: obj
                            }, true);

                            form.submit();
                        }
                    }
                }

                return;
            }

            this.data.removeKey(key);
        },

        /**
        * Toolbars on destroy event handler.
        * @method onDestroy
        * @private
        */
        onDestroy: function() {
            delete this.data;
            var tb = this.component;
            if (tb != null) {
                tb.removeAll(true);
            }

            View.superclass.onDestroy.apply(this, arguments);
        }
    });

    net.autodata.compare.toolbars.PageToolbarView = View;
})();
